﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QunProject.Common;
using EnumHelper = QunProject.Model.EnumHelper;

namespace QunProject.Manage.Controllers
{
    /// <summary>
    /// 功能管理
    /// </summary>
    public class SysMenuController : BaseController
    {
        // 首页
        public ActionResult Index()
        {
            return View();
        }
        // 获取数据
        public JsonResult AjaxData()
        {
            var list = EF.sys_menu.Where(x => true);

            var data = list.Select(x => new
            {
                open = true,
                id = x.ID,
                pId = x.Pid.Value,
                name = x.Title + (x.IsValid.Value ? "" : " ×")
            }).ToList();

            data.Add(new { open = true, id = -1L, pId = 0, name = "点击此处添加顶级节点" });

            return Json(Result.Success(data));
        }
        // 提交编辑
        public JsonResult AjaxEdit(Model.sys_menu model)
        {
            Model.sys_menu _model = new Model.sys_menu();
            if (!Common.Verify.VerifyLength(model.Title, 2, 8))
                return Json(Result.Fail("名称长度为2-8"));

            if (model.ID > 0)
            {
                _model = EF.sys_menu.SingleOrDefault(x => x.ID == model.ID);
                _model = EF.sys_menu.Attach(_model);
            }
            else
            {
                _model.CreateTime = DateTime.Now;
                _model.Pid = model.Pid;
            }
            _model.Type = model.Type;
            _model.Title = model.Title;
            _model.Url = model.Url;
            _model.Remark = model.Remark;
            _model.Img = model.Img;
            _model.Sort = model.Sort;
            _model.IsValid = model.IsValid;

            bool isSucc = false;
            if (model.ID > 0)
            {
                isSucc = EF.SaveChanges() > 0;
            }
            else
            {
                EF.sys_menu.Add(_model);
                isSucc = EF.SaveChanges() > 0;
            }
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 提交删除
        public JsonResult AjaxDelete(string ids)
        {
            var delIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (delIds.Length == 0)
                return Json(Result.Fail("请选择需要删除的内容"));

            foreach (var id in delIds)
            {
                var modelId = id.ToInt();
                var model = EF.sys_menu.SingleOrDefault(x => x.ID == modelId);
                model.IsValid = false;
            }

            bool isSucc = EF.SaveChanges() > 0;
            return isSucc ? Json(Result.Success()) : Json(Result.Fail());
        }
        // 查询实体
        public JsonResult AjaxEntity(int id)
        {
            var model = EF.sys_menu.SingleOrDefault(x => x.ID == id);
            if (model == null)
                return Json(Result.Fail("不存在此管理员"));
            return Json(Result.Success(model));
        }
    }
}